package com.spsoft.vms.redis.service.impl;

import com.spsoft.vms.redis.constant.RedisConstant;
import com.spsoft.vms.redis.service.VmsQueueService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RQueue;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 上传成功后生成板式文件队列服务
 * 需要上传的发票，把发票号码放入队列
 * @author cdl
 * @date 2024/03/13
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class InvoiceFileCreateVmsQueueServiceImpl implements VmsQueueService<String> {

    private final RedissonClient redisson;

    @Override
    public boolean addMessageToQueue(String message) {
        RQueue<String> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD_SUCCESS);
        if(queue.contains(message)) {
            return true;
        }
        return queue.offer(message);
    }

    @Override
    public String getMessageFromQueue() {
        RQueue<String> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD_SUCCESS);
        return queue.poll();
    }

    @Override
    public List<String> getMessagesFromQueue(int count) {
        RQueue<String> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD_SUCCESS);
        return queue.poll(count);
    }

    @Override
    public int getQueueSize() {
        RQueue<String> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD_SUCCESS);
        return queue.size();
    }

    @Override
    public boolean containsMessage(String message) {
        RQueue<String> queue = redisson.getQueue(RedisConstant.QUEUE_INVOICE_UPLOAD_SUCCESS);
        return  queue.contains(message);
    }
}
